<template>
  <el-dialog
    title="分配角色"
    :visible="dialogVisibleRole"
    width="60%"
    @close="close"
  >
    ==={{ roleIds }}
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="btnOk">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getRoleListApi, userInfoDetailApi, assignRolesApi } from "@/api/user";
export default {
  name: "",
  props: {
    dialogVisibleRole: {
      type: Boolean
    },
    userId: {
      type: String
    }
  },
  data() {
    return {
      roleIds: [], //  选中的角色
      roleList: [] //  所有的角色
    };
  },
  created() {
    this.getRole();
  },
  methods: {
    async getRole(id) {
      //  获取所有的角色
      const { rows } = await getRoleListApi();
      /* 根据id获取角色 */

      //console.log(res, 2223344);
      this.roleList = rows;
    },
    async getRolesById(id) {
      const { roleIds } = await userInfoDetailApi(id);
      this.roleIds = roleIds;
    },
    close() {
      this.$emit("update:dialogVisibleRole", false);
      this.roleIds = [];
      //this.$emit('qq',false)
    },

    /*分配角色 */
    async btnOk() {
      await assignRolesApi({
        id: this.userId,
        roleIds: this.roleIds
      });
      this.close();
    }
  }
};
</script>
<style lang="less" scoped></style>
